package com.jackeysun.log

import android.content.Context
import android.content.ContextWrapper
import android.content.res.Resources
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.stardust.enhancedfloaty.FloatyService
import com.stardust.enhancedfloaty.ResizableExpandableFloaty
import com.stardust.enhancedfloaty.ResizableExpandableFloatyWindow

/**
 *   Created by sunxunchao on 2021/12/14.
 */
class ConsoleFloaty : ResizableExpandableFloaty.AbstractResizableExpandableFloaty() {

    private var mContextWrapper: ContextWrapper? = null
    private var mResizer: View? = null
    private var mMoveCursor: View? = null
    private var mTitleView: TextView? = null
    private var mTitle: CharSequence? = null
    private var mExpandedView: View? = null

    init {
        setShouldRequestFocusWhenExpand(false)
        initialX = 100
        initialY = 1000
        collapsedViewUnpressedAlpha = 1.0f
    }

    override fun getInitialWidth(): Int {
        return WindowManager.LayoutParams.WRAP_CONTENT //ScreenMetrics.getDeviceScreenWidth() * 2 / 3;
    }

    override fun getInitialHeight(): Int {
        return WindowManager.LayoutParams.WRAP_CONTENT //ScreenMetrics.getDeviceScreenHeight() / 3;
    }

    override fun inflateCollapsedView(
        service: FloatyService?,
        window: ResizableExpandableFloatyWindow?
    ): View {
        ensureContextWrapper(service)
        return View.inflate(mContextWrapper, R.layout.floating_window_collapse, null)
    }

    override fun inflateExpandedView(
        service: FloatyService?,
        window: ResizableExpandableFloatyWindow?
    ): View {
        ensureContextWrapper(service)
        val view = View.inflate(mContextWrapper, R.layout.floating_console_expand, null)
        setListeners(view, window)
        setUpConsole(view, window)
        initConsoleTitle(view)
        setInitialMeasure(view)
        mExpandedView = view
        return view
    }

    private fun setUpConsole(view: View, window: ResizableExpandableFloatyWindow?) {
        val consoleView: ConsoleView = view.findViewById(R.id.console)
        consoleView.autoLog()
    }

    private fun ensureContextWrapper(context: Context?) {
        if (mContextWrapper == null) {
            mContextWrapper = ContextThemeWrapper(context, R.style.ConsoleTheme)
        }
    }

    private fun initConsoleTitle(view: View) {
        mTitleView = view.findViewById(R.id.title)
        mTitleView?.text = mTitle
    }

    private fun setInitialMeasure(view: View) {
        view.post {
            setViewMeasure(
                view, screenWidth() * 2 / 3,
                screenHeight() / 3
            )
        }
    }

    private fun setViewMeasure(view: View, width: Int, height: Int) {
        val params = view.layoutParams
        params.width = width
        params.height = height
        view.layoutParams = params
    }

    private fun screenWidth(): Int {
        return Resources.getSystem().displayMetrics.widthPixels
    }

    private fun screenHeight(): Int {
        return Resources.getSystem().displayMetrics.heightPixels
    }

    private fun setListeners(view: View, window: ResizableExpandableFloatyWindow?) {
        setWindowOperationIconListeners(view, window)
    }

    private fun setWindowOperationIconListeners(
        view: View,
        window: ResizableExpandableFloatyWindow?
    ) {
        view.findViewById<View>(R.id.close).setOnClickListener { v: View? -> window?.close() }
        view.findViewById<View>(R.id.move_or_resize).setOnClickListener { v: View? ->
            if (mMoveCursor?.visibility == View.VISIBLE) {
                mMoveCursor?.visibility = View.GONE
                mResizer?.visibility = View.GONE
            } else {
                mMoveCursor?.visibility = View.VISIBLE
                mResizer?.visibility = View.VISIBLE
            }
        }
        view.findViewById<View>(R.id.minimize).setOnClickListener { v: View? -> window?.collapse() }
    }

    fun setTitle(title: CharSequence) {
        mTitle = title
        mTitleView?.text = title
    }

    override fun getResizerView(expandedView: View): View? {
        mResizer = expandedView.findViewById(R.id.resizer)
        return mResizer
    }

    override fun getMoveCursorView(expandedView: View): View? {
        mMoveCursor = expandedView.findViewById(R.id.move_cursor)
        return mMoveCursor
    }
}